﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace InterfaceView
{
    public partial class Operation : CCSkinMain
    {
        public Operation()
        {
            InitializeComponent();
        }

        private void Operation_Load(object sender, EventArgs e)
        {
            skinDataGridView1.DataSource = BusinessLogic.RM.dgv();
          
        }

        private void skinButton1_Sure_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void skinButton2_No_Click(object sender, EventArgs e)
        {
            this.Close();
            Operation childop = new Operation();
            childop.Show();
        }

        private void skinButton1_Query_Click(object sender, EventArgs e)
        {
           
            BusinessLogic.RM.RoomManagementOfQuery(this.skinDataGridView1,skinComboBox1.SelectedItem.ToString(),skinTextBox1_selecet.Text);
            this.skinTextBox1_selecet.Text = "";
        }

        private void skinButton1_addRoom_Click(object sender, EventArgs e)
        {
            rmAddroom childadd = new rmAddroom();
            childadd.Show();
        }

        private void skinButton1_delete_Click(object sender, EventArgs e)
        {
            string ID = Convert.ToString(skinDataGridView1.SelectedRows[0].Cells[0].Value);
            string id = ID;
            if(BusinessLogic.RM.DeleteRoom(ID)!=null&&BusinessLogic.RM.DeleteRoomStatus(id)!=null)
            {
                MessageBox.Show("Have already Deleted ! ");
            }
        }

        private void skinButton1_modify_Click(object sender, EventArgs e)
        {
            RMModify childrmm = new RMModify();
            childrmm.Show();
        }

        private void skinDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void skinButton1_query2_Click(object sender, EventArgs e)
        {
            BusinessLogic.Query.Queryof2(this.skinDataGridView2, skinComboBox2.SelectedItem.ToString(), skinTextBox1_selectquery.Text);
            this.skinTextBox1_selectquery.Text = "";
        }

        private void skinLabel2_Click(object sender, EventArgs e)
        {

        }

        private void skinButton1_addperson_Click(object sender, EventArgs e)
        {
            if (validateInput())
            {
                string roomId = this.skinTextBox1_id.Text;
                string roomType = this.skinComboBox3_type.SelectedItem.ToString();
                string roomPrice = this.skinTextBox2_price.Text;
                string personName = this.skinTextBox3_name.Text;
                string personID = this.skinTextBox4_pid.Text;
                string personSex = this.skinRadioButton1.Text.ToString();
                string checkinDate = this.skinDateTimePicker1.Text.ToString();
                string roomStatus = this.skinComboBox3_inrs.SelectedItem.ToString();
                if (BusinessLogic.Checkin.AddPerson(roomId, roomType, roomPrice, personName, personID, personSex, checkinDate, roomStatus) != null)
                {
                    BusinessLogic.Checkin.updateroom();
                    MessageBox.Show("person  have add to succeed !");
                    this.skinTextBox1_id.Text = "";
                    this.skinComboBox3_type.Text = "";
                    this.skinTextBox2_price.Text = "";
                    this.skinTextBox3_name.Text = "";
                    this.skinTextBox4_pid.Text = "";
                    this.skinDateTimePicker1.Text = "";
                    this.skinComboBox3_inrs.Text = "";
                    IsAdd isadd = new IsAdd();
                    isadd.Show();
                }

            }
        }

        public bool validateInput()
        {
            if (this.skinTextBox1_id.Text.Length == 0 || this.skinTextBox2_price.Text.Length ==0 ||this.skinTextBox3_name.Text.Length == 0)
            {
                MessageBox.Show("User information is incomplete! ");
                this.skinTextBox1_id.Focus();
                return false;
            }
            return true;
        }

        private void skinButton1_outroom_Click(object sender, EventArgs e)
        {
            string roomId = this.skinTextBox1_out_id.Text;
            string roomPrice = this.skinTextBox3_out_rp.Text;
            string personName = this.skinTextBox2_out_pname.Text;
            string checkinDate = this.skinDateTimePicker2_outcd.Text.ToString();
            string checkoutDate = this.skinDateTimePicker3_od.Text.ToString();
            string roomStatus = this.skinComboBox3_outrs.SelectedItem.ToString();
            
            if(BusinessLogic.Checkout.OutRoom(roomId,roomPrice,personName,checkinDate,checkoutDate,roomStatus)!=null)
            {
                DateTime d1 = Convert.ToDateTime(checkinDate);
                DateTime d2 = Convert.ToDateTime(checkoutDate);
                int days = (d2 - d1).Days;
                double price = days *( Convert.ToDouble(roomPrice));
                MessageBox.Show("Check Out Successfully！Please pay off :  " + price.ToString() +" $ ");
                BusinessLogic.Checkin.updateroom();
                this.skinTextBox1_out_id.Text = "";
                this.skinTextBox3_out_rp.Text = "";
                this.skinTextBox2_out_pname.Text = "";
                this.skinDateTimePicker2_outcd.Text = "";
                this.skinDateTimePicker3_od.Text = "";
                this.skinComboBox3_outrs.Text = "";
                


            }
        }

        private void skinButton1_qq_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://wpa.qq.com/msgrd?v=3&uin=437860365&site=qq&menu=yes");
        }

        
    }
}
